<?php
/**
 * Akimimi Front
 * CategorySearchView
 * by Steve
 */

require_once '../app/views/BaseView.php';
class SearchResultView extends BaseView {
	
	
	public function getPage($groups,$brands,$products,$gNumArr,$cNumArr,$bNumArr,$pnum,$headerPaginate,$keywords,$cid,$cName,$brand,$priceArr,$paginate){
		if($paginate){
			 $page_content = $this->getTopPaginate($pnum,$headerPaginate,$keywords,$cName,$brand)."<div id='class_hr'></div>".$this->getProductList($products,$headerPaginate);
        } else {
		$this->setPageTitle($keywords);
        $this->addCSS('search_result.css');
        $this->addJS('search_result.js');
     
        
        $str_content = 
<<<str_content
            <div id="navigation"><a href="/">首页</a><span>></span>搜索结果</div>
            <input type="hidden" id="keywords" value="{$keywords}" />
            <input type="hidden" id="selected_cid" value="{$cid}" />
            <input type="hidden" id="selected_brand" value="{$brand}" /> 
            <div id="c_body">
                {$this->getLeftNav($groups,$gNumArr,$cNumArr,$brands,$bNumArr,$keywords,$cid,$brand,$priceArr)}
               <div id="c_right">
                   {$this->getTopPaginate($pnum,$headerPaginate,$keywords,$cName,$brand)}
                   <div id="class_hr"></div>
                   {$this->getProductList($products,$headerPaginate)}
               </div>  
            </div>
            
str_content;
        
        $page_content = $this->getHtmlPage($str_content);
        }
        return $page_content;
	}
	
	private function getLeftNav($groups,$gNumArr,$cNumArr,$brands,$bNumArr,$keywords,$selected_cid,$selected_brand,$priceArr){
		$str_group = '';
		foreach($groups as $gid=>$group){
			$str_category = '';
			$backUrl = '/s/'.$keywords;
			foreach($group['category'] as $cid=>$category){
			   $url = '/s/'.$keywords.'/cid/'.$cid;
			   if($selected_cid == $cid ){
			   	    $str_category .= " <p onclick='javascript:Search.gotoUrl(\"{$url}\");'>{$category['title']}({$cNumArr[$cid]})</p>";
			   	  	$str_category .= "<p onclick='javascript:Search.gotoUrl(\"{$backUrl}\");' >返回</p>";
			   	  	break;
			  } else {
				     $str_category .= "<p onclick='javascript:Search.gotoUrl(\"{$url}\");' >{$category['title']}({$cNumArr[$cid]})</p>";
			  }
			}
			   $str_group .=
<<<str_group
              <p class="t_1">{$group["name"]}({$gNumArr[$gid]})</p> 
              {$str_category} 
                
str_group;
		}
		$str_brand = '';
		foreach($brands as $brand){
			$url = '/s/'.$keywords.'/brand/'.urlencode($brand);
			$cut_num = 14-(mb_strlen($brand, "gb2312")-mb_strlen($brand, "utf-8"));
			$brandShow = (mb_strlen($brand, "gb2312") > 16)?CommonUtil::cut_str($brand,$cut_num): $brand;
			$str_brand .= "<p style='width:140px;' onclick='javascript:Search.gotoUrl(\"{$url}\");' >{$brandShow}({$bNumArr[$brand]})</p>";
			if($selected_brand == $brand){
				$str_brand .= "<p onclick='javascript:Search.gotoUrl(\"{$backUrl}\");' >返回</p>";
				break;
			}
			
			
		}
		
		$str_nav = 
<<<str_nav
       <div id="class_left">
    	 <div class="cb_top"></div>  
         <div class="cb_center">
        	<div class="c_title_1">搜索结果</div>
            <div class="c_body">
            	<div class="c_t"></div>
                <div class="c_c">
                	<div class="list_title">
                    	<h3>按类别搜索结果</h3>
                        <div class="list_title_b">
                          {$str_group}                                                   
                        </div>                        
                    </div>
                    <div class="list_title">
                    	<h3>按品牌搜索结果</h3>
                        <div class="list_title_b">
                          {$str_brand}
                        </div>
                    </div>
                	<div class="list_title" style=" margin-bottom:7px;">
                    	<h3>按价格搜索结果</h3>
                        <div class="list_title_s">
                            <h3>价格范围</h3>
                            <form action="{$backUrl}" name="price_search_form" id="price_search_form" method="post">
                            <p class="s_1">
                            	<input type="text" id="lowestPrice" name="lowestPrice"  class="text_number" value="{$priceArr[0]}" />
                                 --
                                <input type="text" id="highestPrice" name="highestPrice"  class="text_number" value="{$priceArr[1]}" />
                            </p>
                            <p class="s_2"><span style="cursor:pointer;" onclick="javascript:price_search_form.submit();">确定</span></p>
                            </form>
                        </div> 
                    </div>                                          
                </div>
                <div class="c_b"></div>
            </div>
        </div>
        <div class="cb_bottom"></div>
           {$this->getAd()}
     </div>
str_nav;
		return $str_nav;
	}
	
	private function getAd(){
		$str_ad = "";
		return $str_ad;
	}
	
	private function getProductList($products,$headerPaginate){
		$str_product = '';
		foreach($products as $product){
			$image = empty($product['name'])?ConfigConstants::getDefaultProductImagePath('160'):ConfigConstants::getProductImagePath($product['folder'],$product['name'],'160');
			$p_title = (strlen($product['title']) > 36)?CommonUtil::cut_str($product['title'],36): $product['title'];
			$p_url = "/p/".urlencode($product['url_title']);
			$str_product .=
<<<str_product
             <div class="pro_list">
                  <img src="{$image}" alt="" style="cursor:pointer" onclick="window.open('{$p_url}');"/>
                  <p class="title_1" style="cursor:pointer" onclick="window.open('{$p_url}');">{$p_title}</p>
                  <p class="title_2">市场价格：￥{$product['marketing_price']}</p>
                  <p class="title_2">米米价格：<span>￥</span><b>{$product['list_price']}</b></p>
             </div>
str_product;
		}
		$str_productlist = 
<<<str_productlist
      <div id="class_product">
        {$str_product}
        <div class="clear"></div>
        {$this->getBottomPaginate($headerPaginate)}                  
    </div>
    <div id="product_hr">
        <div class="s_nav_b" style="margin-left:1px; width:756px;"></div>
        <div class="s_nav_b" style="margin-left:2px; width:754px; background:#CBCBCB;"></div>    
    </div> 
    
str_productlist;
          return $str_productlist;
	}
	
	private function getTopPaginate($pnum,$headerPaginate,$keywords,$cName,$brand){
		if($cName){
			$range = $cName;
		} else if($brand){
			$range = $brand;
		} else {
			$range = "所有分类";
		}
		$cut_num = 16-(mb_strlen($keywords, "gb2312")-mb_strlen($keywords, "utf-8"))-mb_strlen($range, "utf-8");
		$variableLength = mb_strlen($keywords, "gb2312")+mb_strlen($range, "gb2312");
		if($variableLength > 24 && $cut_num < 6 ){
			$keywordsShow = CommonUtil::cut_str($keywords,6);
			$range = CommonUtil::cut_str($range,6);
		} else {
		    $keywordsShow = ($variableLength > 24)?CommonUtil::cut_str($keywords,$cut_num): $keywords;
		}
	
		$str_toppaginate = 
<<<str_toppaginate
           <div id="s_nav">
             <div class="s_nav_b" style="margin-left:2px; width:754px; background:#CBCBCB;"></div>
             <div class="s_nav_b" style="margin-left:1px; width:756px;"></div>
             <div class="s_nav_t">
                 <div class="right_nav" >在<span>{$range}</span>中搜索<span>{$keywordsShow}</span>共有<span>{$pnum}</span>种商品</div>
                 <div class="page_nav">
            	     <div class="p2_page" style="margin-top:-10px; margin-left:60px;">{$headerPaginate}</div>
                 </div>
             </div>
          </div>
          <div id="product_hr">
             <div class="s_nav_b" style="margin-left:1px; width:756px;"></div>
             <div class="s_nav_b" style="margin-left:2px; width:754px; background:#CBCBCB;"></div>    
          </div>  
str_toppaginate;
		return $str_toppaginate;
	}
	
	private function getBottomPaginate($headerPaginate){
		$str_bottompaginate =
<<<str_bottompaginate
           <div class="bottom_page">
               <div class="p2_page" style="margin-top:-10px;margin-left:-60px;">{$headerPaginate}</div>
          </div> 
str_bottompaginate;
       return $str_bottompaginate;
	}
}

